$(document).ready(function(){
    $(document).on("click", "#new_criterion", function(){
       last_id = $(".criterion").last().attr("id");
       if(last_id === undefined)
           new_counter = 0;
       else
           new_counter = parseInt(last_id.match(/[0-9]+/))+1;
       $("<tr><td><div><textarea class='criterion' id='user_story_criterions_attributes_"+new_counter+"_criterion' name='user_story[criterions_attributes]["+new_counter+"][criterion]'></textarea></div></td><td><div><input type='checkbox' id='user_story_criterions_attributes_"+new_counter+"_complied' name='user_story[criterions_attributes]["+new_counter+"][complied]'></div></td><td><div id='deleteCriterion'><a href='javascript:void(null)'><img alt='Dieses Akzeptanzkriterium l&amp;ouml;schen' class='icon' id='eraseIcon' src='/assets/layout/Erase.png'></a></div></td></tr>").appendTo($(".acceptabilityCriterion"));
    });
    $(document).on("click", "#deleteCriterion", function(e){
        e.preventDefault();
        marked_id = $(this).children("input").attr("value");
        marked_item = $(this);
        showDialog('warning', "L&ouml;schen des Akzeptanzkriterium", "Das Akzeptanzkriterium wird unwiederruflich gelöscht!<p><a href=\"javascript:void(null)\" id=\"confirmCriterionDelete\"><img alt=\"confirmIcon\" class=\"icon\" id=\"confirmIcon\" src=\"/assets/layout/OK.png\"/> Best&auml;tigen</a><a href=\"javascript:void(null)\" id=\"cancel\" class=\"closeButton\"><img alt=\"cancelIcon\" class=\"icon\" id=\"cancelIcon\" src=\"/assets/layout/Cancel.png\"/> Abbrechen</a></p>");
    });
	
    $(document).on("click", "#confirmCriterionDelete", function(){
        $("#shader").hide();
        $.getJSON("../user_stories/delete_criterion?id="+marked_id).done(function(found){		
            if( found == 1 ){
                $(marked_item).parent().parent().remove();
            }
            else if(found == 0) {
                $(marked_item).parent().parent().remove();
            }
            else {
                alert("Beim L&ouml;schen ist folgender Fehler aufgetreten: " + found);
            }
        });
    });
});